<?php

/** 
 * @property App_version_mdl $version
 * @property ApiOutput $apiOutput
 * @property Browser $browser
 */
class App extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		$this->load->model('App_version_mdl','version');
	}
	/**
	 * 获取APP配置参数
	 * 	传入参数:
	 * 		system:系统(ios/android)
	 */
	public function setting(){
		$buyCycle = 1;//买水通知周期
		$devPrefix =  array('RJ','runjoy','USR','RX');//蓝牙设备前缀
		$unAcessBtFreeTime = 10;//未授权蓝牙设备空闲时间
		
		$system  = $this->input->get_post('system',TRUE);
		if(  ! in_array($system, array('ios','android')) ){
			$this->apiOutput->errorInput();
		}
		$lastVersion = $this->version->getLastVersion($system);
		unset($lastVersion['id']);
		unset($lastVersion['system']);
		$extensions = array(
// 				array(
// 						'icon'=>'http://heshui-api.yxlady.com/images/icons/icon_price_small.png',
// 						'url'=>'http://heshui-api.yxlady.com/app/price',
// 						'title'=>'价格表',
// 				),
// 				array(
// 						'icon'=>'http://heshui-api.yxlady.com/images/icons/icon_price_small.png',
// 						'url'=>'http://heshui-api.yxlady.com',
// 						'title'=>'测试2',
// 				),
// 				array(
// 						'icon'=>'http://heshui-api.yxlady.com/images/applogo.png',
// 						'url'=>'http://heshui-api.yxlady.com',
// 						'title'=>'测试3',
// 				),
		);
		
		$this->apiOutput->put('buyCycle', $buyCycle)
						->put('devPrefix', $devPrefix)
						->put('version', $lastVersion)
						->put('extension', $extensions)
						->put('unAcessBtFreeTime', $unAcessBtFreeTime)
						->out();
	}
	
	
	
	/**
	 * 检查版本API
	 * 传入参数：内部版本号
	 */
	public function check_android(){
		$input = check_input($_REQUEST,array('version'));
		if( !$input ){
			$this->apiOutput->errorInput();
		}
		$internal_version = $input['version'];
	
		$lastVersion = $this->version->getLastVersion('android');
	
		if( $internal_version < $lastVersion['internal_version'] ){
			unset($lastVersion['id']);
			unset($lastVersion['upload_time']);
			$this->apiOutput->put('status', 1)
							->put('data',$lastVersion)
							->out();
		}else{
			$this->apiOutput->put('status', 0)->msg('您当前已经是最新版本！');
		}
	
	}
	
	public function check_ios(){
		$version = (int)$this->input->post_get('version');
		if (empty($version)) {
			$this->apiOutput->errorInput();
		}
		$lastVersion = $this->version->getLastVersion('ios');
		if( $version < $lastVersion['internal_version'] ){
			$this->apiOutput->put('status', 0)
							->put('version', $lastVersion)
							->put('msg', '抱歉,当前版本已停止使用,请到AppStore更新最新版本!')
							->out();
		}else{
			$this->apiOutput->put('status', 1)->out();
		}
	}
	
	public function download(){
		$this->load->library('Browser',NULL,'browser');
		$system = $this->browser->isIOS ? 'ios' : 'android';
		$temp   = array();
		$temp['system'] = $system;
		
		if ( $this->browser->isWechat ) {
			$this->load->view('download/wechat',$temp);
			return true;
		}
		if( $system=='ios' ){
			header("Location:https://itunes.apple.com/gb/app/yi-dong-cai-bian/id1146090418?mt=8");
			exit;
		}
		$version = $this->version->getLastVersion($system);
		
		$temp['version'] = $version;
		$this->load->view('download/index',$temp);
	}
	
	public function about(){
		$this->load->view('app/about');
	}
	
	public function price(){
		$this->load->model('Water_price_mdl','price');
		$prices['normal'] = $this->price->getPrice(BUY_MODE_NORMAL);
		$prices['once']   = $this->price->getPrice(BUY_MODE_ONCE);
		$temp['prices'] = $prices;
		$this->load->view('app/price',$temp);
	}
	
	
	
	
	
	
	
	
	
	
	
}

